package com.huobao.yunying.activitys;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huobao.yunying.adapters.Adapter_SeeHistory;
import com.huobao.yunying.base.HuobaoApplication;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.entity.MySeeHistory;
import com.huobao.yunying.utils.SkipUtils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;

import org.xutils.DbManager;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

public class Act_SeeHistory extends HuobaoBaseActivity implements OnItemClickListener {

	private Adapter_SeeHistory adapter;
	@ViewInject(R.id.gridview)
	private GridView gridview;
	private DbManager db;
	

	@Override
	protected int getContentView() {
		return R.layout.layout_seehistory;
	}

	@Override
	protected String getTitleText() {
		return "我的足迹";
	}

	@Override
	protected void init() {
		mAbTitleBar.addRightView(rghtView());
		HuobaoApplication application = (HuobaoApplication) getApplication();
		db = x.getDb(application.daoConfig);
		try{
			List<MySeeHistory> list = db.findAll(MySeeHistory.class);
			adapter = new Adapter_SeeHistory(context, list);
			gridview.setAdapter(adapter);
			gridview.setOnItemClickListener(this);
		}catch (Exception e){}

	}

	private View rghtView() {
		LinearLayout layout = new LinearLayout(context);
		TextView textView = new TextView(this);
		textView.setText("清空");
		textView.setPadding(8, 4, 8, 4);
		textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,16);
		textView.setTextColor(Color.parseColor("#ffffff"));
		textView.setBackgroundResource(R.drawable.selector_commect_text_btn);
		textView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				BaseDialog.TBox("是否清空足迹？", context, new BaseDialog.OnDialogSelectListener() {
					@Override
					public void onSelect(String input) {
						if(input.equals("true")){
							try{
								db.delete(MySeeHistory.class);
							}catch (Exception e){}
							adapter.clear();
						}
					}
				});
			}
		});
		layout.addView(textView);
		return layout;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		SkipUtils.seeDetail(context, adapter.getItem(position).getGoods_id(),
				adapter.getItem(position).getDefault_img());
	}
}
